package cn.zhoujing.demo.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author zhoujing
 * @createTime 2023/11/19 - 15:07
 */
@Configuration
public class TokenConfig {

    @Autowired
    public RedisConnectionFactory redisConnectionFactory;

    /**
     * token存储策略
     * <p>
     * InMemoryTokenStore：将token存储至内存中
     * JdbcTokenStore：将token存储至数据库中
     * RedisTokenStore：将token存储至Redis中
     * JwtTokenStore：将token转换为jwt
     *
     * @return
     */
    @Bean
    public TokenStore tokenStore() {
       RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
       redisTokenStore.setPrefix("auth-token:");
       return redisTokenStore;
    }

    @Bean
    public JwtAccessTokenConverter tokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        tokenConverter.setSigningKey("jwt_token");
        return tokenConverter;
    }


}
